package main;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;


public class Menu extends JMenuBar {

	private JMenu file = new JMenu("File");
	private JMenuItem newScore = new JMenuItem("New");
	private JMenuItem save = new JMenuItem("Save");
	private JMenuItem open = new JMenuItem("Open");
	private JMenuItem view = new JMenuItem("View File");

	private JMenu help = new JMenu("Help");
	private JMenuItem helpMe = new JMenuItem("Help...");
	private JLabel helpLabel;
	private QNote parent;


	/**
	 * Creates the menu bar
	 * @param p parent frame
	 */
	public Menu(QNote p){
		parent = p;
		file.add(newScore);
		newScore.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				new NewScoreScreen(parent); 
			}
		});
		file.addSeparator();
		file.add(save);
		file.add(open);
		file.addSeparator();
		file.add(view);

		// Display Help screen
		// Read in the help text file
		Scanner reader = null;
		try {reader = new Scanner(new FileReader("help_text.txt"));} catch (FileNotFoundException e) {}
		String helpText = "";
		while(reader.hasNextLine()){
			helpText += reader.nextLine();
			helpText += '\n';
		}

		helpLabel = new JLabel(helpText);
		helpMe.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				callHelpScreen();
			}
		});
		//end Help Screen display

		save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveFile();
			}
		});

		open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openFile();
			}
		});

		view.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				showScoreCode();
			}
		});


		help.add(helpMe);

		add(file);
		add(help);
	}

	/**
	 * Pulls up the help screen
	 */
	public void callHelpScreen(){
		JFrame helpPane = new JFrame();
		helpPane.setLayout((new FlowLayout(FlowLayout.LEADING, 5, 0)));
		helpPane.add(helpLabel);
		helpPane.pack();
		helpPane.setResizable(false);
		helpPane.setLocation(parent.getWidth()+15, 0);
		helpPane.setLocationRelativeTo(parent);
		helpPane.setVisible(true);
	}//end callHelpScreen

	/**
	 * Saves the file as a txt and a png
	 */
	public void saveFile(){
		if (parent.getCurrentScore() == null){
			parent.showErrorMessage();
			return;
		}
		String code = parent.getCurrentScore().getScoreCode();
		String name = parent.getCurrentScore().getName();
		try{
			// Create file 
			FileWriter fstream = new FileWriter(name + ".txt");
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(code);
			//Close the output stream
			out.close();
		}
		catch (Exception e){}
		printImage();
		JOptionPane.showMessageDialog(null, "Your file has now been saved as a txt and png file named: " + parent.getCurrentScore().getName(), "File saved", JOptionPane.INFORMATION_MESSAGE);
	}//end saveFile

	/**
	 * Opens a txt file and pulls it onscreen
	 */
	public void openFile(){
		String text = "";
		String fileName = JOptionPane.showInputDialog(this, "Type file name here.");
		File file = new File(fileName);
		try{
			Scanner scan = new Scanner(file);
			while(scan.hasNextLine()){
				String line = scan.nextLine();
				text+=line;
				text+="\n";
			}
			scan.close();
		}
		catch(FileNotFoundException e)
		{

		}

		parent.interpret(text);
	}//end openFile

	/**
	 * Opens a CodeViewer module
	 */
	public void showScoreCode(){
		if (parent.getCurrentScore() == null){
			parent.showErrorMessage();
			return;
		}
		new CodeViewer(parent.getCurrentScore().getScoreCode(), parent);

	}

	/**
	 * saves the image as a png
	 */
	public void printImage(){
		String name = parent.getCurrentScore().getName();
		try{
			// retrieve image
			BufferedImage bi = parent.getCurrentScore().getScore();
			File outputfile = new File(name + ".png");
			ImageIO.write(bi, "png", outputfile);
		} 
		catch (IOException e){
		}
	}
}
